// a1 引入模块
const Koa = require('koa')
// a5
const Router = require('koa-router')
// b7
const poolSql = require('./bd.js') // 数据库
const cors = require('koa2-cors') // 跨域
const bodyparser = require('koa-bodyparser') // 获得body参数

const nowTime = require('./time.js')
// a2 实例化
const app = new Koa()
// a6
const router = new Router()

// a3 搭建服务
// app.use(async ctx => {
//   ctx.body = 'xxxx0000'
// })

// a6
/* router.get('/index', async ctx => {
  ctx.status = 200 // 不写也会默认200，204
  ctx.body = 'xxx000'
}) */

router.get('/index', async a6 => {
  a6.status = 200 // 不写也会默认200，204
  a6.body = 'xxx000'
})
router.get('/index2', async ctx => {
  ctx.status = 200 // 不写也会默认200，204
  ctx.body = 'xxx000222'
})
// 查询
router.get('/query', async ctx => {
  try {
    let _sql = 'SELECT * FROM a1'
    let _data = await poolSql(_sql)
    ctx.body = {
      errorMessage: '查询成功',
      result: true,
      data: _data
    }
  } catch (err) {
    ctx.body = {
      errorMessage: '未查询到',
      result: false,
      data: null
    }
  }
})
// b10 添加
router.post('/add', async ctx => {
  let _info = ctx.request.body

  // b15
  console.log('ctx.request', ctx.request)
  // b10.2
  if (!_info.name) {
    ctx.body = {
      errorMessage: '名称未填',
      result: false,
      data: null
    }
    // b10.3
    return
  }

  // b11
  try {
    // b12
    let _sql = 'INSERT INTO a1 (name,number,price,time) VALUES (?,?,?,?)'
    // _info.time = nowTime
    let _value = [_info.name, _info.number, _info.price, nowTime]

    await poolSql(_sql, _value)
    // b16 b17
    // console.log('ctx.request2', ctx.request.body)
    ctx.body = {
      errorMessage: '添加成功',
      result: true,
      data: null
    }
  } catch (err) {
    ctx.body = {
      errorMessage: '添加失败',
      result: false,
      data: null
    }
  }
})
// b13 修改
router.put('/put', async ctx => {
  // b14
  let info = ctx.request.body
  // b17
  if (!info.id) {
    ctx.body = {
      errorMessage: 'id是必须的',
      result: false,
      data: null
    }
    return
  } else if (!info.name) {
    ctx.body = {
      errorMessage: '名称未填',
      result: false,
      data: null
    }
    // b10.3
    return
  }
  try {
    // b18
    let sql = 'update a1 set name=?,number=?,price=?,time=? where id=?'
    let val = [info.name, info.number, info.price, nowTime, info.id]
    await poolSql(sql, val)
    ctx.body = {
      errorMessage: '修改成功',
      result: true,
      data: null
    }
  } catch {
    ctx.body = {
      errorMessage: '修改失败',
      result: false,
      data: null
    }
  }
})

//b19 删除
router.delete('/del/:id', async ctx => {
  console.log('ctx', ctx.params)
  console.log('router.routes()',router.routes())
  let info = ctx.params
  try {
    let sql = 'delete from a1 where id=?'
    let val = [info.id]
    await poolSql(sql, val)
    ctx.body = {
      errorMessage: '删除成功',
      result: true,
      data: null
    }
  } catch (error) {
    ctx.body = {
      errorMessage: '删除失败',
      result: false,
      data: null
    }
  }
})

// b20
// router.use('/api',router.routes());

// a7 配置路由 b8
app.use(cors()).use(bodyparser()).use(router.routes()).use(router.allowedMethods())
// a4 监听端口
app.listen(5000, () => {
  console.log('后台启动成功：5000')
})
